
#include <iostream>
using namespace std;
int Bits(int m, int n, int c){return ((c >> n) & ~(-2 << (m - n)));}

char* Utf8Encoder(const char* input, int len){
    char* output = new char[(len * 3) + 0];
    int r0 = 0;int state = 0;
    int pos = 0;
    for (int i = 0; i < len; i++){
        int c = (int)input[i];
        switch (state){
        case (0): {
            if ((Bits(15,7,c)==0)) {
                output[pos++] = ((char)c);state = 0;
            } else {
                if ((!(Bits(15,7,c)==0)&&(Bits(15,11,c)==0))) {output[pos++] = ((char)(0xC0|Bits(10,6,c)));output[pos++] = ((char)(0x80|Bits(5,0,c)));state = 0;} else {if ((!(Bits(15,11,c)==0)&&((0xD800>c)||(c>0xDFFF)))) {output[pos++] = ((char)(0xE0|Bits(15,12,c)));output[pos++] = ((char)(0x80|Bits(11,6,c)));output[pos++] = ((char)(0x80|Bits(5,0,c)));state = 0;} else {if (((0xD800<=c)&&(c<=0xDBFF))) {output[pos++] = ((char)(0xF0|Bits(4,2,(1+Bits(9,6,c)))));output[pos++] = ((char)(((8|((1+Bits(7,6,c))&3))<<4)|Bits(5,2,c)));r0 = Bits(1,0,c);state = 1;} else {throw ("InvalidInput");}}}
            }
            break;}
        case (1): {
            if (((0xDC00<=c)&&(c<=0xDFFF))) {
                output[pos++] = ((char)((((((Bits(11,4,r0)<<1)|1)<<3)|Bits(2,0,r0))<<4)|Bits(9,6,c)));output[pos++] = ((char)(0x80|Bits(5,0,c)));r0 = 0;state = 0;
            } else {
                throw ("InvalidInput");
            }
            break;}
        default: {
            throw ("Utf8Encoder");
                 }
        }
    }
    switch (state){
    case (0): {

        break;}
    case (1): {
        throw ("InvalidInput");
        break;}
    default: {
        throw ("Utf8Encoder");
             }
    }
    return output;
}
